/**
 * Object Util
 * @author ZhangBosong
 * @date 2019-3-1 13:08:04
 */
export class ObjectUtils {

  /**
   * 返回只读属性配置
   * @param value 属性值
   */
  static readonlyPropDescripter(value: any): PropertyDescriptor {
    return {
      value,
      configurable: false,
      enumerable: true,
      writable: false,
    };
  }

  /**
   * 深度合并两个对象
   * @param object1
   * @param object2
   */
  static deepObjectMerge(object1: any, object2: any) {
    for (const key in object2) {
      if (object2.hasOwnProperty(key)) {
        object1[key] = object1[key] && object1[key].toString() === '[object Object]' ?
          this.deepObjectMerge(object1[key], object2[key]) : object1[key] = object2[key];
      }
    }
    return object1;
  }

  /**
   * 深度合并多个对象
   * @param objs
   */
  static deepAssign(...objs) {
    if (objs && objs.length) {
      if (objs.length === 1) {
        return {...objs[0]};
      }
      let first = objs[0];
      for (let i = 1; i < objs.length; i++) {
        const next = objs[i];
        if (!next) {
          continue;
        }
        first = this.deepObjectMerge(first, next);
      }
      return first;
    }
    return null;
  }

  /**
   * 对象不等于null、undefined
   * @param o
   */
  static isNotNull(o: any): boolean {
    return o !== null && o !== undefined;
  }

}
